package com.guoqi.keepsign.bxs

import android.content.Context
import android.graphics.Color
import android.os.Bundle
import android.support.v4.app.Fragment
import android.support.v4.app.FragmentStatePagerAdapter
import android.support.v4.content.ContextCompat
import android.support.v4.view.PagerAdapter
import android.support.v4.view.ViewPager
import android.support.v7.app.AppCompatActivity
import android.view.View
import android.view.animation.AccelerateInterpolator
import android.view.animation.DecelerateInterpolator
import com.guoqi.keepsign.IPConfig
import com.guoqi.keepsign.R
import kotlinx.android.synthetic.main.ui_bxs_tab.*
import net.lucode.hackware.magicindicator.ViewPagerHelper
import net.lucode.hackware.magicindicator.buildins.UIUtil
import net.lucode.hackware.magicindicator.buildins.commonnavigator.CommonNavigator
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.CommonNavigatorAdapter
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerIndicator
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerTitleView
import net.lucode.hackware.magicindicator.buildins.commonnavigator.indicators.LinePagerIndicator
import net.lucode.hackware.magicindicator.buildins.commonnavigator.titles.SimplePagerTitleView
import java.util.*

class BxsTabUI : AppCompatActivity() {
    private lateinit var newsFragment: BxsFm
    private lateinit var bosomFragment: BxsFm
    private lateinit var stockingFragment: BxsFm
    private lateinit var buttocksFragment: BxsFm
    private lateinit var legsFragment: BxsFm
    private lateinit var prettyFragment: BxsFm

    private val tabList = Arrays.asList("最新", "大胸妹", "小翘臀", "黑丝袜", "美腿控", "有颜值")
    private lateinit var mAdapter: PagerAdapter
    private var pageViews = ArrayList<Fragment>()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.ui_bxs_tab)

        //隐藏nav
//        val decorView = window.decorView
//        val option = View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
//        decorView.systemUiVisibility = option
//        window.navigationBarColor = Color.TRANSPARENT

        //隐藏底栏
//        val params = window.attributes
//        params.systemUiVisibility = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
//        window.attributes = params

        //初始化页面
        initViewPager()
        initIndicator()
    }

    private fun initViewPager() {
        newsFragment = BxsFm.newInstance(IPConfig.BXE_ALL)
        bosomFragment = BxsFm.newInstance(IPConfig.BXE_BOSOM)
        buttocksFragment = BxsFm.newInstance(IPConfig.BXE_BUTTOCKS)
        stockingFragment = BxsFm.newInstance(IPConfig.BXE_STOCKINGS)
        legsFragment = BxsFm.newInstance(IPConfig.BXE_LEGS)
        prettyFragment = BxsFm.newInstance(IPConfig.BXE_PRETTY)
        pageViews.add(newsFragment)
        pageViews.add(bosomFragment)
        pageViews.add(buttocksFragment)
        pageViews.add(stockingFragment)
        pageViews.add(legsFragment)
        pageViews.add(prettyFragment)
        setViewPagerAdapter()
    }

    private fun initIndicator() {
        val commonNavigator = CommonNavigator(this)
        commonNavigator.scrollPivotX = 0.35f
        commonNavigator.adapter = object : CommonNavigatorAdapter() {
            override fun getCount(): Int {
                return tabList?.size ?: 0
            }

            override fun getTitleView(context: Context, index: Int): IPagerTitleView {
                val tv_tab = SimplePagerTitleView(context)
                tv_tab.text = tabList[index]
                tv_tab.textSize = 16f
                tv_tab.paint.isFakeBoldText = true
                tv_tab.normalColor = Color.parseColor("#9e9e9e")
                tv_tab.selectedColor = ContextCompat.getColor(this@BxsTabUI, R.color.colorAccent)
                tv_tab.setOnClickListener { viewPager.currentItem = index }
                return tv_tab
            }

            override fun getIndicator(context: Context): IPagerIndicator {
                val indicator = LinePagerIndicator(context)
                indicator.mode = LinePagerIndicator.MODE_WRAP_CONTENT
                indicator.lineHeight = UIUtil.dip2px(context, 3.0).toFloat()
                indicator.lineWidth = UIUtil.dip2px(context, 20.0).toFloat()
                indicator.roundRadius = UIUtil.dip2px(context, 3.0).toFloat()
                indicator.startInterpolator = AccelerateInterpolator()
                indicator.endInterpolator = DecelerateInterpolator(2.0f)
                indicator.setColors(ContextCompat.getColor(this@BxsTabUI, R.color.colorAccent))
                return indicator
            }
        }
        magicIndicator.navigator = commonNavigator
        ViewPagerHelper.bind(magicIndicator, viewPager)
    }

    private fun setViewPagerAdapter() {
        mAdapter = object : FragmentStatePagerAdapter(supportFragmentManager) {

            override fun getItem(arg0: Int): Fragment {
                return pageViews[arg0]
            }

            override fun getCount(): Int {
                return pageViews.size
            }
        }

        viewPager.adapter = mAdapter
        viewPager.currentItem = 0
        viewPager.offscreenPageLimit = 1
        viewPager.addOnPageChangeListener(object : ViewPager.OnPageChangeListener {

            override fun onPageSelected(i: Int) {
                magicIndicator.onPageSelected(i)
            }

            override fun onPageScrolled(i: Int, arg1: Float, arg2: Int) {
                magicIndicator.onPageScrolled(i, arg1, arg2)
            }

            override fun onPageScrollStateChanged(arg0: Int) {
                magicIndicator.onPageScrollStateChanged(arg0)
            }
        })
    }


//    override fun onWindowFocusChanged(hasFocus: Boolean) {
//        super.onWindowFocusChanged(hasFocus)
//        if (hasFocus) {
//            val decorView = window.decorView
//            decorView.systemUiVisibility = (View.SYSTEM_UI_FLAG_LAYOUT_STABLE
//                    or View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
//                    or View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
//                    or View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
//                    or View.SYSTEM_UI_FLAG_FULLSCREEN
//                    or View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY)
//        }
//    }
}

//    private class MyHandler(activity: SplashActivity) : Handler() {
//        private val mActivity: WeakReference<SplashActivity> = WeakReference(activity)
//
//        override fun handleMessage(msg: Message) {
//            if (mActivity.get() == null) {
//                return
//            }
//            when (msg.what) {
//                SWITCH_MAINACTIVITY -> {
//                    Log.e(TAG, "SplashActivity --- switch_MainActivity")
//                }
//                else -> {
//                }
//            }
//        }
//    }